package com.xbongbong.paas.help.menu;

import com.xbongbong.admin.pojo.dto.MirrorRollBackDTO;
import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.domain.entity.PaasAppEntity;
import com.xbongbong.paas.domain.entity.PaasFormEntity;
import com.xbongbong.paas.domain.entity.PaasMenuEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.model.PaasAppModel;
import com.xbongbong.paas.model.PaasFormModel;
import com.xbongbong.paas.model.PaasMenuModel;
import com.xbongbong.paas.pojo.dto.FormEntityAddDTO;
import com.xbongbong.paas.pojo.dto.MenuGroupAddDTO;
import com.xbongbong.paas.service.PaasFormService;
import com.xbongbong.paas.service.PaasMenuService;
import com.xbongbong.paas.service.init.AbstractInitMenuHelp;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.enums.AccessLinkEnum;
import com.xbongbong.pro.enums.DistributorMarkEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.enums.menu.enums.ProductPriceMenuEnum;
import com.xbongbong.pro.form.pojo.dto.FormAddBatchDTO;
import com.xbongbong.pro.form.pojo.vo.FormAddBatchVO;
import com.xbongbong.pro.menu.pojo.dto.MenuGroupAddBatchDTO;
import com.xbongbong.pro.menu.pojo.dto.MenuInitDTO;
import com.xbongbong.pro.menu.pojo.vo.MenuGroupAddBatchVO;
import com.xbongbong.pro.menu.pojo.vo.MenuInitVO;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Component
public class InitPriceMenuHelp extends AbstractInitMenuHelp {

    private static final Logger LOG = LoggerFactory.getLogger(InitPriceMenuHelp.class);

    @Resource
    private PaasMenuService paasMenuService;
    @Resource
    private PaasFormService paasFormService;
    @Resource
    private PaasAppModel paasAppModel;
    @Resource
    private PaasMenuModel paasMenuModel;
    @Resource
    private PaasFormModel paasFormModel;

    @Override
    public List<String> alias() {
        return new ArrayList<>();
    }
    @Override
    public MenuInitVO initApp(MenuInitDTO menuInitDTO, Map<String, PaasAppEntity> appMap) throws XbbException {
        return null;
    }

    @Override
    public MenuInitVO initMenu(MenuInitDTO menuInitDTO, Map<String, PaasAppEntity> appMap) throws XbbException {
        MenuInitVO menuInitVO = new MenuInitVO();
        try {
            menuInitDTO.setDistributorMark(DistributorMarkEnum.DISTRIBUTOR.getCode());
            // 获取下应用id
            PaasAppEntity paasAppEntity = paasAppModel.getByAlias(XbbRefTypeEnum.PRODUCT.getAlias(), menuInitDTO.getCorpid());
            menuInitDTO.setAppId(paasAppEntity.getId());
            // 插入除分组外对应的菜单
            MenuGroupAddBatchVO menuFormAddBatchVO = formAddBatch(menuInitDTO);
            if (Objects.isNull(menuFormAddBatchVO)) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
            }
            // 如果已经初始化过了就跳过插入form
            PaasFormEntityExt paasFormEntityExt = paasFormModel.getByBusinessType4Distributor(XbbRefTypeEnum.PRICE_SET_PRODUCT.getCode(), menuInitDTO.getCorpid(), DistributorMarkEnum.DISTRIBUTOR.getCode(), null);
            if (Objects.nonNull(paasFormEntityExt)) {
                return menuInitVO;
            }
            List<PaasMenuEntity> menuFormList = menuFormAddBatchVO.getMenuList();
            // 插入菜单对应的表单
            FormAddBatchDTO formAddBatchDTO = new FormAddBatchDTO();
            BeanUtil.copyProperties(menuInitDTO, formAddBatchDTO);
            List<FormEntityAddDTO> formList = new ArrayList<>();
            // 通过分组菜单，得到需要插入的表单列表
            getFormList(formList, menuFormList);
            formAddBatchDTO.setFormList(formList);
            FormAddBatchVO formAddBatchVO = paasFormService.addBatch(formAddBatchDTO);
            if (Objects.isNull(formAddBatchVO)) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
            }
            // 插入表单对应的字段解释
            menuInitVO.setFormList(formAddBatchVO.getFormList());
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOG.error("InitCrmMenuHelp.menuInit初始化菜单出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return menuInitVO;
    }

    private void getFormList(List<FormEntityAddDTO> formList, List<PaasMenuEntity> menuFormList) throws XbbException {
        try {
            for (PaasMenuEntity item : menuFormList) {
                FormEntityAddDTO formEntityAddDTO = new FormEntityAddDTO();
                BeanUtil.copyProperties(item, formEntityAddDTO);
                ProductPriceMenuEnum productMenuEnum = ProductPriceMenuEnum.getByAlias(item.getAlias());
                if (Objects.isNull(productMenuEnum)) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.BUSINESS_TYPE_NOT_EXIST);
                }
                formEntityAddDTO.setBusinessType(productMenuEnum.getCode());
                formEntityAddDTO.setSaasMark(SaasMarkEnum.SAAS.getCode());
                formEntityAddDTO.setLabels(new ArrayList<>());
                formEntityAddDTO.setCustomTitle(new ArrayList<>());
                formEntityAddDTO.setSummaryEntity(new ArrayList<>());
                formEntityAddDTO.setBusinessRules("0");
                formEntityAddDTO.setMenuId(item.getId());
                formList.add(formEntityAddDTO);
            }
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOG.error("InitCrmMenuHelp.getFormList 出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
    }

    private MenuGroupAddBatchVO formAddBatch(MenuInitDTO menuInitDTO) throws Exception {
        MenuGroupAddBatchVO menuGroupAddBatchVO = new MenuGroupAddBatchVO();
        try {
            Long appId = menuInitDTO.getAppId();
            PaasMenuEntity paasMenuEntity = paasMenuModel.getByAlias4Distributor(XbbRefTypeEnum.PRICE_SET_PRODUCT.getAlias(), menuInitDTO.getCorpid(), DistributorMarkEnum.DISTRIBUTOR.getCode());
            if (Objects.nonNull(paasMenuEntity)) {
                if (Objects.equals(paasMenuEntity.getEnable(), 0)) {
                    paasMenuEntity.setEnable(1);
                    paasMenuModel.update(paasMenuEntity);
                }
                menuGroupAddBatchVO.setMenuList(Collections.singletonList(paasMenuEntity));
                return menuGroupAddBatchVO;
            }
            MenuGroupAddBatchDTO menuGroupAddBatchDTO = new MenuGroupAddBatchDTO();
            BeanUtil.copyProperties(menuInitDTO, menuGroupAddBatchDTO);
            menuGroupAddBatchDTO.setAppId(appId);
            List<MenuGroupAddDTO> groupMenuList = new ArrayList<>();
            ProductPriceMenuEnum menuEnum = ProductPriceMenuEnum.getByCode(XbbRefTypeEnum.PRICE_SET_PRODUCT.getCode());
            if (StringUtil.isEmpty(menuEnum.getParent())) {
                MenuGroupAddDTO menuGroupAddDTO = new MenuGroupAddDTO();
                BeanUtil.copyProperties(menuInitDTO, menuGroupAddDTO);
                menuGroupAddDTO.setParentId(0L);
                menuGroupAddDTO.setAppId(appId);
                menuGroupAddDTO.setAlias(menuEnum.getAlias());
                menuGroupAddDTO.setSaasMark(SaasMarkEnum.SAAS.getCode());
                menuGroupAddDTO.setName(menuEnum.getName());
                AccessLinkEnum accessLinkEnum = menuEnum.getAccessLinkEnum();
                menuGroupAddDTO.setIcon(accessLinkEnum.getIcon());
                menuGroupAddDTO.setColor(accessLinkEnum.getColor());
                menuGroupAddDTO.setUrl(accessLinkEnum.getUrl());
                menuGroupAddDTO.setMobileUrl(accessLinkEnum.getMobileUrl());
                menuGroupAddDTO.setType(menuEnum.getType());
                groupMenuList.add(menuGroupAddDTO);
            }
            menuGroupAddBatchDTO.setMenuList(groupMenuList);
            menuGroupAddBatchVO = paasMenuService.addBatchGroupMenu(menuGroupAddBatchDTO);
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOG.error("InitCrmMenuHelp.groupAddBatch批量添加分组出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return menuGroupAddBatchVO;
    }

    @Override
    public void rollBackInit(MirrorRollBackDTO mirrorRollBackDTO, List<Long> installFormList, PaasFormEntity productFormEntity) throws XbbException {

    }
}
